﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Models;
using DbControl;

namespace CentralMonitoring
{
    /// <summary>
    /// 规则信息编辑窗体
    /// </summary>
    public partial class RuleParamAdd : Form
    {
        public rules mPara = null;
        public OprType mType = OprType.Add;
        public RuleParamAdd()
        {
            InitializeComponent();
        }
        private void InitCombox()
        {
            comboBoxPara.DisplayMember = "parameter_name";
            comboBoxPara.ValueMember = "id";
            comboBoxPara.DataSource = OprParameter.GetParameterAll();           
             
        }
        private void RuleParamAdd_Load(object sender, EventArgs e)
        {
            InitCombox();
            if (mPara != null)
            {
                comboBoxPara.SelectedValue = mPara.parameter_id;
                textBoxrule_code.Text = mPara.rule_code;
                textBoxUpperUpper.Text = mPara.upper_upper_value;
                textBoxUpper.Text = mPara.upper_value.ToString();
                textBoxLower.Text = mPara.lower_value.ToString();
                textBoxLowerLower.Text = mPara.lower_lower_value.ToString();
                comboBoxFlag.SelectedIndex = mPara.flag.Value;
            }
            else
            {                
                if (comboBoxPara.Items.Count > 0)
                    comboBoxPara.SelectedIndex = 0;
                comboBoxFlag.SelectedIndex = 0;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxUpper.Text) || textBoxLower.Text.Length < 1)
            {
                MessageBox.Show("上下限值为必填项！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            rules rules = new rules();
            var paras = comboBoxPara.SelectedItem as parameters;
            rules.parameter_id = paras.id;
            rules.rule_code=textBoxrule_code.Text;
            rules.upper_upper_value = textBoxUpperUpper.Text;
            rules.upper_value = textBoxUpper.Text;
            rules.lower_lower_value = textBoxLowerLower.Text;
            rules.lower_value = textBoxLower.Text;           
            rules.flag=comboBoxFlag.SelectedIndex;
            if (mType == OprType.Add)
            {
                if (OprRules.IsExistRuleCode(rules.rule_code))
                {
                    MessageBox.Show("该规则名已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;

                }
                if (OprRules.Insert(rules) > 0)
                {
                    MessageBox.Show("添加成功!", "提示");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("添加失败！", "提示");
                }

            }
            else
            {
                if (OprRules.IsExistRuleCode(rules.rule_code) && mPara.rule_code != rules.rule_code)
                {
                    MessageBox.Show("规则名已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                try
                {
                    rules.id = mPara.id;
                    if (OprRules.Update(rules) > 0)
                    {
                        MessageBox.Show("更新成功!", "提示");
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("更新失败！", "提示");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("操作失败！" + ex.ToString());
                }
            }
            
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
